<template>
  <div class="app-container">
    <!--订单-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <label class="el-form-item-label">客户编号</label>
        {{ lianxirenval }}
        <label class="el-form-item-label">客户名称</label>
        {{ lkmCustName }}
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="客户名称" prop="odrCustomer">
            <el-input v-model="form.odrCustomer" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="下单日期" prop="odrDate">
            <el-date-picker v-model="form.odrDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="客户地址">
            <el-input v-model="form.odrAddr" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单状态">
            <el-select v-model="form.odrStatus" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.odr_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler" @current-change="handleCurrentChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="odrId" label="订单编号" />
        <el-table-column prop="odrCustomer" label="客户名称" />
        <el-table-column prop="odrDate" label="下单日期" />
        <el-table-column prop="odrAddr" label="客户地址" />
        <el-table-column prop="odrStatus" label="订单状态">
          <template slot-scope="scope">
            {{ dict.label.odr_status[scope.row.odrStatus] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','orders:edit','orders:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <orders-line :sodd-order-id="soddOrderId" />
    </div>
  </div>
</template>

<script>
import ordersLine from '../ordersLine/index.vue'
import crudOrders from '@/api/orders'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { odrId: null, odrCustomer: null, odrDate: null, odrAddr: null, odrStatus: null }
export default {
  name: 'Orders',
  components: { pagination, crudOperation, udOperation, ordersLine },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  props: {
    lianxirenval: {
      type: String,
      default: ''
    },
    lkmCustName: {
      type: String,
      default: ''
    }
  },
  dicts: ['odr_status'],
  cruds() {
    return CRUD({ queryOnPresenterCreated: false, title: '订单信息', url: 'api/orders', idField: 'odrId', sort: 'odrId,desc', crudMethod: { ...crudOrders }})
  },
  data() {
    return {
      soddOrderId: 0,
      permission: {
        add: ['admin', 'orders:add'],
        edit: ['admin', 'orders:edit'],
        del: ['admin', 'orders:del']
      },
      rules: {
        odrCustomer: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ],
        odrDate: [
          { required: true, message: '下单日期不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'odrCustomer', display_name: '客户名称' }
      ]
    }
  },
  watch: {
    'lianxirenval'() {
      this.crud.params = { odrCustomer: this.lkmCustName }
      this.crud.toQuery()
    }
  },
  created() {
    this.crud.params = { odrCustomer: this.lkmCustName }
    this.crud.toQuery()
  },
  methods: {
    handleCurrentChange(val) {
      this.soddOrderId = val.odrId
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
